package com.qyhl.webtv.module_circle.utils.itemview;

import com.google.gson.Gson;
import com.qyhl.webtv.basiclib.utils.DESedeUtil;
import com.qyhl.webtv.basiclib.utils.network.EasyHttp;
import com.qyhl.webtv.basiclib.utils.network.callback.SimpleCallBack;
import com.qyhl.webtv.basiclib.utils.network.exception.ApiException;
import com.qyhl.webtv.basiclib.utils.network.model.ApiResult;
import com.qyhl.webtv.commonlib.common.CommonUtils;
import com.qyhl.webtv.commonlib.entity.circle.CircleHomeBean;
import com.qyhl.webtv.module_circle.common.CircleUrl;
import com.zhy.adapter.abslistview.CommonAdapter;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by qyhl on 2018/1/19.
 */

public class ItemViewModel implements ItemViewContract.ItemModel {
    private ItemViewPresenter mPresenter;
    String userName;
    public ItemViewModel(ItemViewPresenter mPresenter) {
        this.mPresenter = mPresenter;
        userName= CommonUtils.getInstance().getUserName();
    }

    /**
     * 点赞
     *
     * @param topicId
     */
    @Override
    public void doLove(String topicId, final CircleHomeBean bean) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=loveTopic" + "&username=" +userName + "&topicId=" + topicId
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.doLoveSuccess(bean);
                        }
                    }
                });
    }

    /**
     * 取消点赞
     *
     * @param topicId
     */
    @Override
    public void undoLove(String topicId, final CircleHomeBean bean) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;
        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=cancleLove" + "&username=" + userName + "&topicId=" + topicId
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {

                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            mPresenter.undoLoveSuccess(bean);
                        }
                    }
                });


    }

    /**
     * 删除评论
     *
     * @param postId
     */
    @Override
    public void deleteComment(final String postId, final List<CircleHomeBean.PostList> beanList, final CommonAdapter commonAdapter) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
        String time = sdf.format(new Date());
        String code = "Z5" + time + "I2863";
        String url = CircleUrl.CIRCLE_URL + "?timestamp=" + time;

        EasyHttp.post(url)
                .upBytes(DESedeUtil.encryptMode(code, "siteId=" + CommonUtils.getInstance().getSite() + "&method=deletePost" + "&username=" + userName + "&postId=" + postId
                ))
                .isDecrypt(true)
                .decryptKet(code)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onError(ApiException e) {
                        mPresenter.deleteError("网络异常，删除失败！");
                    }

                    @Override
                    public void onSuccess(String s) {
                        ApiResult apiResult = new Gson().fromJson(s,ApiResult.class);
                        if (apiResult.getCode() == 200) {
                            for (int i = 0; i < beanList.size(); i++) {
                                if (beanList.get(i).getId() == Integer.parseInt(postId)) {
                                    beanList.remove(i);
                                }
                            }
                            mPresenter.deleteSuccess(beanList, commonAdapter);
                        } else {
                            mPresenter.deleteError("评论或已删除，请刷新后重试！");
                        }
                    }
                });
    }
}
